<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >

<!-- @author Jin,QingHua -->
<!-- @version ${now?string("yyyy-MM-dd HH:mm:ss")} -->
<sqlMap namespace="${tableName}">

	<typeAlias alias="${domainClassName?uncap_first}" type="${domainPackageName}.${domainClassName}" />

	<cacheModel id="oneDayCache" type="OSCACHE">
		<flushInterval hours="24" />
		<flushOnExecute statement="insert${domainClassName}" />
		<flushOnExecute statement="update${domainClassName}" />
		<flushOnExecute statement="delete${domainClassName}" />
	</cacheModel>

	<resultMap id="${domainClassName?uncap_first}ResultForList" class="${domainClassName?uncap_first}">
<#list columnList as column>
		<result column="${column.columnName}" property="${column.propertyName}" jdbcType="${column.jdbcTypeName}" />
</#list>
	</resultMap>

	<resultMap id="${domainClassName?uncap_first}Result" class="${domainClassName?uncap_first}" extends="${domainClassName?uncap_first}ResultForList">
	</resultMap>

	<sql id="sf-${domainClassName?uncap_first}">
<#list columnList as column>
		<isNotEmpty prepend=" and " property="${column.propertyName}">${column.columnName} = #${column.propertyName}:${column.jdbcTypeName}#</isNotEmpty>
</#list>
	</sql>

	<select id="select${domainClassName}" resultMap="${domainClassName?uncap_first}Result" parameterClass="${domainClassName?uncap_first}" cacheModel="oneDayCache">
		select * from ${tableName} where 1 = 1
		<include refid="sf-${domainClassName?uncap_first}" />
	</select>

	<select id="select${domainClassName}List" resultMap="${domainClassName?uncap_first}ResultForList" parameterClass="${domainClassName?uncap_first}" cacheModel="oneDayCache">
		<isNotEmpty property="row.count">
			<![CDATA[ select * from ( ]]>
		</isNotEmpty>
		select * from ${tableName} where 1 = 1
		<include refid="sf-${domainClassName?uncap_first}" />
		<!-- order by ID asc -->
		<isNotEmpty property="row.count">
			<![CDATA[ ) where rownum <= #row.count# ]]>
		</isNotEmpty>
	</select>

	<select id="select${domainClassName}Count" resultClass="long" parameterClass="${domainClassName?uncap_first}" cacheModel="oneDayCache">
		select count(*) from ${tableName} where 1 = 1
		<include refid="sf-${domainClassName?uncap_first}" />
	</select>

	<select id="select${domainClassName}PaginatedList" resultMap="${domainClassName?uncap_first}Result" parameterClass="${domainClassName?uncap_first}" cacheModel="oneDayCache">
		<![CDATA[ select * from ( select t_.*, rownum rn_ from ( ]]>
		select * from ${tableName} where 1 = 1
		<include refid="sf-${domainClassName?uncap_first}" />
		<!-- order by ID asc -->
		<![CDATA[ ) t_ where rownum <= (#row.first# + #row.count#)) where rn_ >= (#row.first# + 1) ]]>
	</select>

	<insert id="insert${domainClassName}" parameterClass="${domainClassName?uncap_first}">
		<selectKey resultClass="long" keyProperty="id">select SEQ_TAB_${tableName}.nextval as id from dual</selectKey>
		<![CDATA[insert into ${tableName} (]]>
		<dynamic prepend=" ">
<#list columnList as column>
			<isNotNull prepend="," property="${column.propertyName}">${column.columnName}</isNotNull>	
</#list>
		</dynamic>
		<![CDATA[) values (]]>
		<dynamic prepend=" ">
<#list columnList as column>
			<isNotNull prepend="," property="${column.propertyName}">#${column.propertyName}:${column.jdbcTypeName}#</isNotNull>
</#list>
		</dynamic>
		<![CDATA[)]]>
	</insert>

	<update id="update${domainClassName}" parameterClass="${domainClassName?uncap_first}">
		update ${tableName}
		<dynamic prepend="set">
<#list columnList as column>
			<isNotNull prepend="," property="${column.propertyName}">${column.columnName} = #${column.propertyName}:${column.jdbcTypeName}#</isNotNull>
</#list>
		</dynamic>
		where
		<isNotEmpty prepend=" " property="id">ID = #id#</isNotEmpty>
		<isEmpty prepend=" " property="id">
			<isNotEmpty prepend=" " property="map.pks">
				ID in
				<iterate close=")" open="(" conjunction="," property="map.pks">#map.pks[]#</iterate>
			</isNotEmpty>
		</isEmpty>
	</update>

	<delete id="delete${domainClassName}" parameterClass="${domainClassName?uncap_first}">
		delete from ${tableName} where
		<isNotEmpty prepend=" " property="id">ID = #id#</isNotEmpty>
		<isEmpty prepend=" " property="id">
			<isNotEmpty prepend=" " property="map.pks">
				ID in
				<iterate close=")" open="(" conjunction="," property="map.pks">#map.pks[]#</iterate>
			</isNotEmpty>
		</isEmpty>
	</delete>

</sqlMap>